package io.kumao.gateway.service;

import io.kumao.gateway.model.RouteConfig;
import io.kumao.gateway.repository.RouteConfigRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * RouteConfigService
 *
 * @author 崔作利
 */
@Slf4j
@Service
public class RouteConfigService {

    @Resource
    private RouteConfigRepository routeConfigRepository;

    /**
     * getRouteConfigList
     *
     * @return
     */
    public List<RouteConfig> getRouteConfigList() {
        try {
            return routeConfigRepository.selectList();
        } catch (Exception e) {
            log.error("getRouteConfigList error - {}", ExceptionUtils.getStackTrace(e));
        }
        return null;
    }

    /**
     * createRouteConfig
     *
     * @param routeConfig
     * @return
     */
    public boolean createRouteConfig(RouteConfig routeConfig) {
        try {
            int i = routeConfigRepository.insert(routeConfig);
            if (i > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("createRouteConfig error - {}, {}", ExceptionUtils.getStackTrace(e), routeConfig);
        }
        return false;
    }

    /**
     * deleteRouteConfig
     *
     * @param routeID
     * @return
     */
    public boolean deleteRouteConfig(String routeID) {
        try {
            int i = routeConfigRepository.delete(routeID);
            if (i > 0) {
                return true;
            }
        } catch (Exception e) {
            log.error("deleteRouteConfig error - {}, {}", ExceptionUtils.getStackTrace(e), routeID);
        }
        return false;
    }
}
